package lumis.content.control.workflow;

import java.util.Map;
import java.util.TreeMap;

import lumis.content.ContentManagerFactory;
import lumis.content.workflow.WorkflowManager;
import lumis.doui.control.DataBoundControl;
import lumis.doui.control.IClientSideReadableControl;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.XmlUtil;

/**
 * A control used in administration list to filter workflow assignedTo field.
 * 
 * Example use:
 * <xmp>
 * <control:lum_workflowAssignedToFilter id="state" />				
 * </xmp>
 *
 * xml data available for xsl rendering:
 * <xmp>
 * <control id="state" type="lum_workflowAssignedToFilter">
 * 	<data>
 * 		<option value=""></option>
 * 		<option value="00000000D00000000000000000000001" selected="true">admin</option>
 * 	</data>
 * </control>
 * </xmp>
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 5.6.1
 */
@SuppressWarnings("unchecked")
public class WorkflowAssignedToFilterControl extends DataBoundControl implements IClientSideReadableControl 
{
	@Override
	protected boolean fixedIdentifierRequired()
	{
		return true;
	}

	@Override
	public void setRenderData() throws PortalException
	{
		super.setRenderData();

		StringBuilder results = new StringBuilder(500);

		results.append("<data>");
		results.append("<option value=\"\">"+localize("STR_FILTER_OPTION_ALL")+"</option>");

		String onChangeScript = getClientEventHandlerScript("onChange", false);
		if (onChangeScript != null)
			XmlUtil.addTextNode(controlDefinitionNode, "clientEventHandler", "onChange", onChangeScript);

		String controlStringValue = getConverter().convert(String.class, value);

		for(Map.Entry<String, String> entry : getItems().entrySet())
		{
			results.append("<option");
			results.append(" value=\""+entry.getValue()+"\"");

			if(controlStringValue!=null && controlStringValue.equals(entry.getValue()))
				results.append(" selected=\"true\"");

			results.append(">");

			results.append(XmlUtil.encodeXml(entry.getKey()));

			results.append("</option>");
		}

		results.append("</data>");
		XmlUtil.addNodeFromXmlString(controlDefinitionNode, results.toString());
	}

	protected Map<String, String> getItems() throws PortalException
	{
		String serviceInstanceId = douiContext.getRequest().getServiceInstanceConfig().getId();
		WorkflowManager workflowManager = (WorkflowManager) ContentManagerFactory.getWorkflowManager();
		Map<String, String> unsortedItems = workflowManager.getMetadataPrincipals(SessionConfig.getCurrentSessionConfig(), serviceInstanceId, PortalTransactionFactory.getCurrentTransaction());
		
		Map<String, String> items = new TreeMap<String, String>();
		for(Map.Entry<String, String> entry : unsortedItems.entrySet())
			items.put( localize(entry.getValue()), entry.getKey() );

		return items;
	}
}
 